package com.lzx.applib.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.lzx.applib.R;

/**
 * Created by Administrator on 16/11/27.
 */

public class ShapeTextView extends TextView {

    private static final String TAG = "ShapeTextView";

    public int pressAlpha = 0xaf;

    public String text, selectedText, disableText;
    public int radius, topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius;
    public int solidColor, solidDisableColor, solidPressColor, solidSelectedColor, solidSelectedPressColor;
    public int strokeColor, strokeDisableColor, strokePressColor, strokeSelectedColor, strokeSelectedPressColor;
    public int textColor, textDisableColor, textPressColor, textSelectedColor, textSelectedPressColor;
    int strokeWidth;
    boolean isCircle = false;

    Path solidPath, strokePath;
    Paint solidPaint, strokePaint;
    float[] radiusArray;

    public ShapeTextView(Context context) {
        this(context, null);
    }

    public ShapeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ShapeTextView);
        isCircle = array.getBoolean(R.styleable.ShapeTextView_circle, isCircle);

        text = array.getString(R.styleable.ShapeTextView_text);
        selectedText = array.getString(R.styleable.ShapeTextView_selectedText);
        disableText = array.getString(R.styleable.ShapeTextView_disableText);

        radius = array.getDimensionPixelOffset(R.styleable.ShapeTextView_radius, isCircle ? 1000 : 0);
        topLeftRadius = array.getDimensionPixelOffset(R.styleable.ShapeTextView_topLeftRadius, radius);
        topRightRadius = array.getDimensionPixelOffset(R.styleable.ShapeTextView_topRightRadius, radius);
        bottomLeftRadius = array.getDimensionPixelOffset(R.styleable.ShapeTextView_bottomLeftRadius, radius);
        bottomRightRadius = array.getDimensionPixelOffset(R.styleable.ShapeTextView_bottomRightRadius, radius);

        textColor = getTextColors().getDefaultColor();
        solidColor = array.getColor(R.styleable.ShapeTextView_solidColor, Color.TRANSPARENT);
        strokeColor = array.getColor(R.styleable.ShapeTextView_strokeColor, Color.TRANSPARENT);

        textDisableColor = array.getColor(R.styleable.ShapeTextView_textDisableColor,
                Color.argb(0x7f, Color.red(textColor), Color.green(textColor), Color.blue(textColor)));
        solidDisableColor = array.getColor(R.styleable.ShapeTextView_solidDisableColor,
                Color.argb(0x7f, Color.red(solidColor), Color.green(solidColor), Color.blue(solidColor)));
        strokeDisableColor = array.getColor(R.styleable.ShapeTextView_strokeDisableColor, strokeColor);

        textSelectedColor = array.getColor(R.styleable.ShapeTextView_textSelectedColor, textColor);
        solidSelectedColor = array.getColor(R.styleable.ShapeTextView_solidSelectedColor, solidColor);
        strokeSelectedColor = array.getColor(R.styleable.ShapeTextView_strokeSelectedColor, strokeColor);

        textSelectedPressColor = array.getColor(R.styleable.ShapeTextView_textSelectedPressColor, textSelectedColor);
        solidSelectedPressColor = array.getColor(R.styleable.ShapeTextView_solidSelectedPressColor,
                Color.argb(pressAlpha, Color.red(solidSelectedColor), Color.green(solidSelectedColor), Color.blue(solidSelectedColor)));
        strokeSelectedPressColor = array.getColor(R.styleable.ShapeTextView_strokeSelectedPressColor, strokeSelectedColor);

        textPressColor = array.getColor(R.styleable.ShapeTextView_textPressColor, textColor);
        solidPressColor = array.getColor(R.styleable.ShapeTextView_solidPressColor,
                Color.argb(pressAlpha, Color.red(solidColor), Color.green(solidColor), Color.blue(solidColor)));
        strokePressColor = array.getColor(R.styleable.ShapeTextView_strokePressColor, strokeColor);

        strokeWidth = array.getDimensionPixelOffset(R.styleable.ShapeTextView_strokeWidth, 1);
        boolean selected = array.getBoolean(R.styleable.ShapeTextView_selected, false);
        array.recycle();

        radiusArray = new float[]{
                topLeftRadius, topLeftRadius, topRightRadius ,topRightRadius,
                bottomRightRadius, bottomRightRadius, bottomLeftRadius, bottomLeftRadius
        };
        initPaint();
        setBackgroundColor(Color.TRANSPARENT);
//        setGravity(Gravity.CENTER);
        setSelected(selected);
        setEnabled(isEnabled());
        setOnClickListener(onClickListener);
    }

    public void setCircle() {
        bottomLeftRadius = bottomRightRadius = topLeftRadius = topRightRadius = radius = 10000;
        reset();
    }

    private void reset() {
        radiusArray = new float[]{
                topLeftRadius, topLeftRadius, topRightRadius ,topRightRadius,
                bottomRightRadius, bottomRightRadius, bottomLeftRadius, bottomLeftRadius
        };
        solidPath = null;
    }

    public void initPaint() {
        solidPaint = new Paint();
        solidPaint.setColor(solidColor);
        solidPaint.setAntiAlias(true);
        solidPaint.setStyle(Paint.Style.FILL);

        strokePaint = new Paint();
        strokePaint.setColor(strokeColor);
        strokePaint.setAntiAlias(true);
        strokePaint.setStrokeWidth(strokeWidth);
        strokePaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        resetText();
        if (selected) {
            solidPaint.setColor(solidSelectedColor);
            strokePaint.setColor(strokeSelectedColor);
            setTextColor(textSelectedColor);
            setText(selectedText);
        } else {
            solidPaint.setColor(solidColor);
            strokePaint.setColor(strokeColor);
            setTextColor(textColor);
            setText(text);
        }
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        super.setText(text, type);
        if (isEnabled()) {
            if (isSelected()) {
                selectedText = text.toString();
            } else {
                this.text = text.toString();
            }
        } else  {
            disableText = text.toString();
        }
    }

    public void resetText() {
        if (TextUtils.isEmpty(text)) {
            text = getText().toString();
        }
        if (TextUtils.isEmpty(selectedText)) {
            selectedText = text;
        }
        if (TextUtils.isEmpty(disableText)) {
            disableText = text;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (solidPath == null) {
            solidPath = new Path();
            RectF rectF = new RectF();
            rectF.set(strokeWidth, strokeWidth, getWidth()-strokeWidth, getHeight()-strokeWidth);
            solidPath.addRoundRect(rectF, radiusArray, Path.Direction.CW);

            strokePath = new Path();
            RectF strokeRectF = new RectF();
            strokeRectF.set(strokeWidth/2f, strokeWidth/2f, getWidth()-strokeWidth/2f, getHeight()-strokeWidth/2f);
            strokePath.addRoundRect(strokeRectF, radiusArray, Path.Direction.CW);
        }
        canvas.drawPath(solidPath, solidPaint);
        canvas.drawPath(strokePath, strokePaint);
        super.onDraw(canvas);
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        if (solidPaint == null) {
            return;
        }
        resetText();
        if (enabled) {
            setSelected(isSelected());
        } else {
            solidPaint.setColor(solidDisableColor);
            strokePaint.setColor(strokeDisableColor);
            setTextColor(textDisableColor);
            setText(disableText);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        if (!isEnabled()) return false;

        if (event.getAction() == MotionEvent.ACTION_DOWN ||
                event.getAction() == MotionEvent.ACTION_MOVE) {
            if (isSelected()) {
                solidPaint.setColor(solidSelectedPressColor);
                strokePaint.setColor(strokeSelectedPressColor);
                setTextColor(textSelectedPressColor);
            } else {
                solidPaint.setColor(solidPressColor);
                strokePaint.setColor(strokePressColor);
                setTextColor(textPressColor);
            }
        } else if (event.getAction() == MotionEvent.ACTION_UP ||
                event.getAction() == MotionEvent.ACTION_CANCEL){
            setSelected(isSelected());
        }
        return true;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (isCircle) {
            setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));
            int childWidthSize = getMeasuredWidth();
            int childHeightSize = getMeasuredHeight();
            //height is the same with width
            heightMeasureSpec = widthMeasureSpec =
                    MeasureSpec.makeMeasureSpec(Math.min(childWidthSize, childHeightSize), MeasureSpec.EXACTLY);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            setSelected(!isSelected());
        }
    };

    public void copy(ShapeTextView view) {
        radius = view.radius;
        topLeftRadius = view.topLeftRadius;
        topRightRadius = view.topRightRadius;
        bottomLeftRadius = view.bottomLeftRadius;
        bottomRightRadius = view.bottomRightRadius;

        solidColor = view.solidColor;
        solidDisableColor = view.solidDisableColor;
        solidPressColor = view.solidPressColor;
        solidSelectedColor = view.solidSelectedColor;
        solidSelectedPressColor = view.solidSelectedPressColor;

        strokeColor = view.strokeColor;
        strokeDisableColor = view.strokeDisableColor;
        strokePressColor = view.strokePressColor;
        strokeSelectedColor = view.strokeSelectedColor;
        strokeSelectedPressColor = view.strokeSelectedPressColor;

        textColor = view.textColor;
        textDisableColor = view.textDisableColor;
        textPressColor = view.textPressColor;
        textSelectedColor = view.textSelectedColor;
        textSelectedPressColor = view.textSelectedPressColor;

        strokeWidth = view.strokeWidth;

        setTextSize(TypedValue.COMPLEX_UNIT_PX, view.getTextSize());
        setSelected(view.isSelected());
        reset();
    }
}
